﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <script src="/js/header.js"></script>
    <link href="/lib/ztree/ztreestyle.css" rel="stylesheet" />
    <link href="/lib/icheck/skins/all.css" rel="stylesheet" />
    <style>
        .usermenu {
            background-color: #f5f5f5;
            padding: 5px 20px;
            overflow-x: hidden;
            overflow-y: auto;
            height: 365px;
        }
    </style>
    <script type="text/javascript" src="/lib/ztree/jquery.ztree.all.js"></script>

</head>
<body style="background-color: white;">
    <section class="container-fluid" style="background-color: white;">

        <ul class="nav nav-tabs m15_t" id="myTab">
            <li class="active"><a href="#home" data-toggle="tab">基本信息</a></li>
            <li><a href="#permissions" data-toggle="tab">角色权限</a></li>
        </ul>
        <form class="form-horizontal m15_t" id="form" onsubmit="return false;">
            <div class="tab-content">
                <input type="hidden" id="rId" name="rId" value="0">
                <div class="tab-pane fade in active" id="home">
                    <div class="form-group">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>角色名称：</label>
                        <div class="wd250 m15_l">
                            <input class="form-control" type="text" name="rName" maxlength="40" autocomplete="off" placeholder="长度必须介于 3 和 40 之间">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>所属公司：</label>
                        <div class="wd250 m15_l m5_t">
                            &nbsp;<span id="companyName" style="font-weight:bold;"></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>目标公司：</label>
                        <div class="wd250 m15_l m5_t">
                            <select class="form-control input-sm wd240" name="targetCompany" style="display: inline-block; width: auto;">
                                <option value="0">根目录</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="wd100 control-label"></label>
                        <div class="wd250 m15_l">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="wd100 control-label"></label>
                        <div class="wd250 m15_l">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="wd100 control-label"></label>
                        <div class="wd250 m15_l">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="wd100 control-label"></label>
                        <div class="wd250 m15_l">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="wd100 control-label"></label>
                        <div class="wd250 m15_l">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="wd100 control-label"></label>
                        <div class="wd250 m15_l">
                        </div>
                    </div>

                    <div class="form-group" style="margin-top:10px;padding: 10px 15px;border-top: 1px solid #e5e6e7;">
                        <label class="wd100 control-label"></label>
                        <div class="wd250 m15_l">
                            <button class="btn btn-primary m50_l" onclick="return save();">保存</button>
                            <button class="btn btn-default m10_l" onclick="return cancel();">取消</button>
                        </div>
                    </div>
                </div>
                <!--菜单-->
                <div class="tab-pane fade" id="permissions">
                    <div class="form-group">
                        <div class="usermenu">
                            <div class="zTreeDemoBackground left">
                                <ul id="ztree" class="ztree"></ul>
                            </div>
                        </div>
                    </div>
                </div>

            </div>

        </form>

    </section>

    <script type="text/javascript">

        var validator = null;

        var targetCompany = $("select[name='targetCompany']");

        var setting = {
            check: {
                enable: true,
                chkboxType: { "Y": "ps", "N": "ps"}
            },
            data: {
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "pId",
                    rootPId: 0
                }
            }
        };

        function init(id) {

            var cover = FlashPay.UI.Mask({
                obj: $("body"),
                opacity: 0.5
            });

            FlashPay.Util.Ajax({
                type: "POST",
                url: "/Role/GetClone/" + id,
                data: null,
                success: function (response) {
                    if (response.success) {

                        FlashPay.Util.SetInputVal("rName", response.data.rName)

                        $("span#companyName").html(response.data.companyName);

                        targetCompany.empty();
                        if (response.data.companyResponse != null) {

                            targetCompany.append("<option value='' selected>请选择</option>");
                            $.each(response.data.companyResponse, function (i, item) {
                                targetCompany.append("<option value='" + item.companyID + "'>" + item.companyName + "</option >");
                            });

                            cover.Remove();
                        }
                        else {
                            cover.Remove();
                        }

                    } else {

                    }
                },
                error: function (e) {
                    cover.Remove();
                    FlashPay.UI.Tip_short_warning("操作失败！请查看网络，请重试。");
                }
            });
        }

        $(function () {

            var id = FlashPay.Util.GetQueryString("id");

            init(id);

            validator = FlashPay.Util.Validator({
                formObj: $("#form"),
                rules: {
                    rName: {
                        required: true,
                        rangelength: [3, 40]
                    },
                    targetCompany: {
                        required: true
                    }
                },
                messages: {
                    rName: {
                        required: "长度必须介于 3 和 40 之间",
                        rangelength: "长度必须介于 3 和 40 之间",
                    },
                    targetCompany: {
                        required: "请选择目标公司"
                    }
                },
            });

            InitZTree();
        })
        var ztree;
        //初始化ZTree
        function InitZTree() {

            var roleSysNo = $("input[name='rId']").val();

            FlashPay.Util.Ajax({
                type: "POST",
                url: "/Role/GetMenuPermissionTree",
                data: { mType: FlashPay.Util.Platform, roleSysNo: FlashPay.Util.GetQueryString("id") },
                success: function (response) {
                    if (response != null) {
                        //更具返回结果初始化zTree
                        ztree = $.fn.zTree.init($("#ztree"), setting, response);
                        //默认为收起所以节点
                        ztree.ExpandAllStatus = false;
                    }
                },
                error: function (e) {
                    FlashPay.UI.Tip_short_warning("操作失败！请查看网络，请重试。");
                }
            });
        }

        function save() {

            if (!validator.form()) {
                return false;
            }

            var cover = FlashPay.UI.Mask({
                obj: $("body"),
                opacity: 0.5
            });

            var role = {};
            role.rId = $("input[name='rId']").val();
            role.rName = $("input[name='rName']").val();
            role.rCompanyId = $("select[name='targetCompany']").find("option:selected").val();
            
            var selectedMenuNodes = ztree.getCheckedNodes(true);
            var unselectedMenuNodes = ztree.getCheckedNodes(false);

            var authorizeArray = new Array();
            if (selectedMenuNodes.length > 0) {
                for (var i = 0; i < selectedMenuNodes.length; i++) {
                    var authorizeId = selectedMenuNodes[i].id.replace("m_", "").replace("p_", "");
                    var authorizeType = selectedMenuNodes[i].nodetype;

                    var authorize = {
                        authPid: authorizeId,
                        authRid: role.rId,
                        authType: authorizeType,
                    };
                    authorizeArray.push(authorize);
                }
            }

            FlashPay.Util.Ajax({
                url: '/Role/Clone',
                type: 'POST',
                data: { role: role, authorize: authorizeArray},
                success: function (response) {
                    if (response.success) {

                        cover.Remove();

                        FlashPay.UI.DialogOpener().Tip_success("操作成功！");
                        FlashPay.UI.DialogOpener().doSearch();
                        FlashPay.UI.CloseDialog();
                    } else {
                        cover.Remove();
                        FlashPay.UI.Tip_short_warning("操作失败！" + response.errorMessage);
                    }
                },
                error: function (json) {
                    cover.Remove();
                    FlashPay.UI.Tip_short_warning("操作失败！请查看网络，请重试。");
                }
            });
        }

        function cancel() {
            FlashPay.UI.CloseDialog();
        }
    </script>
</body>
</html>